package com.ruoyi.biz.letter.read;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;

import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.ruoyi.biz.category.service.IBizCategoryService;
import com.ruoyi.biz.letter.util.CategoryUtil;
import com.ruoyi.common.core.domain.entity.BizCategory;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author Fei.Cong
 * @date 2024-07-17 10:50
 */
public class LevelCategoryConverter implements Converter<String> {
    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        String value = "";
        List<BizCategory> categories = CategoryUtil.getCacheCategorys();
        Optional<BizCategory> bizCategory = categories.stream().filter(v -> Objects.equals(v.getCategoryName(), cellData.getStringValue())).findFirst();
        if (bizCategory.isPresent()) {
            value = String.valueOf(bizCategory.get().getId());
        }
        return value;
    }

    @Override
    public WriteCellData convertToExcelData(String value, ExcelContentProperty contentProperty,
                                            GlobalConfiguration globalConfiguration) {
        String str = "";
        List<BizCategory> categories = CategoryUtil.getCacheCategorys();
        Optional<BizCategory> bizCategory = categories.stream().filter(v -> Objects.equals(v.getCategoryName(), value)).findFirst();
        if (bizCategory.isPresent()) {
            str = bizCategory.get().getCategoryName();
        }
        return new WriteCellData(str);
    }
}
